/**
 * DateUtil.java
 * jp.co.greenblue.framework.util
 *
 * Function： TODO 
 *
 *   ver     date      		author
 * ──────────────────────────────────
 *   		 2010/11/03 		jonsion
 *
 * Copyright (c) 2010, GreenBlue .  All Rights Reserved.
*/

package jp.co.greenblue.framework.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;

/**
 * ClassName:DateUtil
 * Function: TODO ADD FUNCTION
 * Reason:	 TODO ADD REASON
 *
 * @author   jonsion
 * @version  
 * @since    Ver 1.1
 * @Date	 2010/11/03		14:14:50
 *
 * @see 	 
 */
public class DateUtil {
	private static final String[] months = new String[] { "Jan", "Feb", "Mar",
		"Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" };

private static final int[] days = new int[] { 31, 28, 31, 30, 31, 30, 31,
		31, 30, 31, 30, 31 };

private static final String weekDays[] = new String[] { "日", "月", "火", "水",
		"木", "金", "土" };
@SuppressWarnings("unused")
private static final int[] monthnum={4,5,6,7,8,9,10,11,12,1,2,3};

private static GregorianCalendar gregorianCalendar=new GregorianCalendar();

/**
 * <b>�?��のすべての時間数返す (四月から三月まで)�?/b><br>
 * 開発時間??2007/09/21<br>
 * 更新時間??2007/09/21<br>
 * 開発人員: jiangxc<br>
 * 
 * @return �?��の時間数
 */
public static int getOneYearAllHours(int year) {
	int hours = 0;
	for (int i = 4; i < 13; i++) {
		hours += getOneMonthAllHours(year, i);
	}
	for (int i = 1; i < 4; i++) {
		hours += getOneMonthAllHours(year + 1, i);
	}
	return hours;
}

/**
 * 取得當前年度?�日本?��??年度為?��?�?日開始，�?��到�?���?�?1日
 * @author jiangxc<br>
 * <b>create Date:</b>2009/10/23 17:08:07<br>
 * <b>last modify Date:</b>2009/10/23 17:08:07<br>
 * @return int 當前年度
 */

public static int getNowNendo() {
	Calendar nowdate=Calendar.getInstance();
	int nendo = 1970;
	int currentYear = nowdate.get(Calendar.YEAR);
	int currentMonth=nowdate.get(Calendar.MONTH)+1;
	if(currentMonth>=4)
		nendo=currentYear;
	else if(currentMonth<=3)
		nendo=currentYear-1;
	return nendo;
}

public static int getNendo(int year,int month) {
	int nendo = 1970;
	int currentYear = year;
	int currentMonth=month;
	if(currentMonth>=4)
		nendo=currentYear;
	else if(currentMonth<=3)
		nendo=currentYear-1;
	return nendo;
}

/**
 * 获得yyyyMMdd格式的当前日期�?
 * @author zhangshiying<br>
 * <b>create Date:</b>2010/05/23 10:14:24<br>
 * <b>last modify Date:</b>2010/05/23 10:14:24<br>
 * @return
 */

public static String getNowDate() {
	String nowDate = getNowDate("yyyyMMdd");
	return nowDate;
}

/**
 * 获得指定�?式的当前日期�?
 * @author zhangshiying<br>
 * <b>create Date:</b>2010/05/23 10:14:28<br>
 * <b>last modify Date:</b>2010/05/23 10:14:28<br>
 * @param pattern
 * @return
 */

public static String getNowDate(String pattern) {
	String nowDate = new SimpleDateFormat(pattern).format(new Date()).toString();
	return nowDate;
}
/**
 * <b>�?��月�?すべての時間数返す�?/b><br>
 * 開発時間??2007/09/21<br>
 * 更新時間??2007/09/21<br>
 * 開発人員: jiangxc<br>
 * 
 * @return �?��月�?すべての時間数
 */
public static int getOneMonthAllHours(int year, int month) {
	int hours = 1;
	int days = 1;
	days = getOneMonthAllDays(year, month);

	hours = days * 24;

	return hours;
}

/**
 * <b>�?��のすべての日返す (四月から三月まで)�?/b><br>
 * 開発時間??2007/09/21<br>
 * 更新時間??2007/09/21<br>
 * 開発人員: jiangxc<br>
 * 
 * @return �?��のすべての日
 */
public static int getOneYearAllDays(int year) {
	int days = 0;
	for (int i = 4; i < 13; i++) {
		days += getOneMonthAllDays(year, i);
	}
	for (int i = 1; i < 4; i++) {
		days += getOneMonthAllDays(year + 1, i);
	}
	return days;
}

/**
 * <b>�?��月�?すべての日返す �?/b><br>
 * 開発時間??2007/09/21<br>
 * 更新時間??2007/09/21<br>
 * 開発人員: jiangxc<br>
 * 変更人員: qq<br>
 * 
 * @return �?��月�?すべての日
 */
public static int getOneMonthAllDays(int year, int month) {
	if (leapYear(year) && month == 2)
		return days[month - 1] + 1;
	else
		return days[month - 1];
}

/**
 * <b>閏年チェ�?���?/b><br>
 * 開発時間??2007/09/21<br>
 * 更新時間??2007/09/21<br>
 * 開発人員: jiangxc<br>
 * 変更人員: qq<br>
 * 
 * @return boolean
 */
public static boolean leapYear(int year) {
	return gregorianCalendar.isLeapYear(year);
	// return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}

// 取得英�?��份名称
// qq 070920 modified
public static String getUSMonthName(int month) {
	return months[month - 1];
}

//取得昭和多少年或平成多少年
public static int getYear2JY(int year)
{
	int	jy;
	jy = year-1988;
	if(jy < 1 )
		return(63 + jy);
	else
		return(jy);
}

// 取得日�?���?�?��
public static String getYearJYStr(int year) {
	int jy;
	jy = year - 1988;
	if (jy < 1)
		return ("昭和");
	else
		return ("平成");
}

//通�?��本人�?��度 取得昭和多少年或平成多少年
public static String getYear2JY(int year,int month)
{	
	if(month==0)
		return "";
	if(month<4)
		year+=1;
	return String.valueOf(getYear2JY(year));
}

//通�?��本人�?��度 取得日�?���?�?��
public static String getYearJYStr(int year,int month) {
	
	if(month==0)
		return "";
	if(month<4)
		year+=1;
	return String.valueOf(getYearJYStr(year));
}

// 取得日�?���?�?��
public static String getYearJYENG(int year) {
	int jy;
	jy = year - 1988;
	if (jy < 1)
		return ("S");
	else
		return ("H");
}

//返回如：昭�?0
public static String getYear2StrAndJY(int year){
	return getYearJYStr(year)+getYear2JY(year);
}
/**
 * <b>返回年</b><br>
 * @author liyujun<br>
 * <b>create Date:</b>2009/09/07 9:38:10<br>
 * <b>last modify Date:</b>2009/09/07 9:38:10<br>
 * @param jyStr 昭和�?平�?
 * @param jyear 年号�?��
 * @return
 */
public static String getYear(String jyStr,int jyear){
	int year=jyear;
	if(jyStr.equals("昭和"))
		year=year+63;
	else if(jyStr.equals("平成"))
		year=year+0;
	year=year+1988;
	return String.valueOf(year);
}

/**
 * <b>返回年度</b><br>
 * @author liyujun<br>
 * <b>create Date:</b>2009/09/07 9:38:10<br>
 * <b>last modify Date:</b>2009/09/07 9:38:10<br>
 * @param jyStr 昭和�?平�?
 * @param jyear 年号�?��
 * @return
 */
public static String getNendo(String jyStr,int jyear,int month){
	int year=jyear;
	if(jyStr.equals("昭和"))
		year=year+63;
	else if(jyStr.equals("平成"))
		year=year+0;
	year=year+1988;
	if(month<4)
		year--;
	return String.valueOf(year);
}

public static String getZhaoHePinchenYMD(String tmpymd){
	String resultstr="平成○○年○月○日";
	if(tmpymd!=null && !"".equals(tmpymd) && tmpymd.length()==8){
		String year=tmpymd.substring(0,4);
		String month=tmpymd.substring(4,6);
		String day=tmpymd.substring(6,8);
		resultstr=getYear2StrAndJY(Integer.parseInt(year))+"年"+month+"月"+day+"日";
	}
	return resultstr;
}

// qq 20070920 modified
public static String getWeek(int year, int month, int day)
		throws ParseException {
	// if (month < 3) {
	// month += 12;
	// --year;
	// }
	// int res = (day + 1 + 2 * month + 3 * (month + 1) / 5 + year
	// + (year >> 2) - year / 100 + year / 400) % 7;
	// String str = weekDays[res];
	return getWeek(String.valueOf(year * 10000 + month * 100 + day));
}

public static long getQuot(String time1, String time2) {
	long quot = 0;
	SimpleDateFormat ft = new SimpleDateFormat("yyyy/MM/dd");
	try {
		Date date1 = ft.parse(time1);
		Date date2 = ft.parse(time2);
		quot = date2.getTime() - date1.getTime();
		quot = quot / 1000 / 60 / 60 / 24;
	} catch (ParseException e) {
		e.printStackTrace();
	}
	return quot;
}

public static String getMonthDay(String month, String year) {
	String day = new String();
	int doYear = Integer.parseInt(year);
	boolean leap;
	if (doYear % 4 != 0)
		leap = false;
	else if (doYear % 100 != 0)
		leap = true;
	else if (doYear % 400 != 0)
		leap = false;
	else
		leap = true;
	if (leap == true && month.equals("02"))
		day = "29";
	else if (leap == false && month.equals("02"))
		day = "28";

	if (month.equals("01") || month.equals("03") || month.equals("05")
			|| month.equals("07") || month.equals("08")
			|| month.equals("10") || month.equals("12")) {
		day = "31";
	} else if (month.equals("04") || month.equals("06")
			|| month.equals("09") || month.equals("11")) {
		day = "30";
	}
	return day;
}

// qq 070920 modified
public static String getMonthDay(int day) {
	return weekDays[day - 1];
}

public static int getDateBysetDays(int year, int month, int day, int valDay) {
	Calendar clr = Calendar.getInstance();
	clr.set(year, month - 1, day);
	clr.add(Calendar.DATE, valDay);
	SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
	return Integer.parseInt(sdf.format(clr.getTime()));
}

/*
 * yyyymmdd
 */
public static String getWeek(String date) throws ParseException {
	Calendar calendar = Calendar.getInstance();
	DateFormat df = new SimpleDateFormat("yyyyMMdd");
	calendar.setTime(df.parse(date));
	return getMonthDay(calendar.get(Calendar.DAY_OF_WEEK)) + "曜日";
}

public static String getWeekNumber(String date) throws ParseException {
	Calendar calendar = Calendar.getInstance();
	DateFormat df = new SimpleDateFormat("yyyyMMdd");
	calendar.setTime(df.parse(date));
	return String.valueOf(calendar.get(Calendar.DAY_OF_WEEK));
}

/**
 * 得到两个日期之间相差�?��数
 * 
 * @param date1
 *            日�?
 * @param date2
 *            日�?
 * @param dateFormat
 *            日期�?�?
 * @return 相差天数
 * @throws ParseException
 */
public static int getDaysDiff(String date1, String date2, String dateFormat)
		throws ParseException {
	Calendar calendar1 = Calendar.getInstance();
	Calendar calendar2 = Calendar.getInstance();

	DateFormat df = new SimpleDateFormat(dateFormat);
	calendar1.setTime(df.parse(date1));
	calendar2.setTime(df.parse(date2));

	int year1 = calendar1.get(Calendar.YEAR);
	int year2 = calendar2.get(Calendar.YEAR);

	int earlyYear = Math.min(year1, year2);
	int lateYear = Math.max(year1, year2);

	Calendar earlyYearCalendar = earlyYear == year1 ? calendar1 : calendar2;
	Calendar lateYearCalendar = earlyYear == year1 ? calendar2 : calendar1;

	if (earlyYear == lateYear) {
		return Math.abs(earlyYearCalendar.get(Calendar.DAY_OF_YEAR)
				- lateYearCalendar.get(Calendar.DAY_OF_YEAR));
	} else {
		int days = 0;
		for (int year = earlyYear; year < lateYear; year++) {
			days += leapYear(year) ? 366 : 365;
		}
		days = days - earlyYearCalendar.get(Calendar.DAY_OF_YEAR)
				+ lateYearCalendar.get(Calendar.DAY_OF_YEAR);
		return days;
	}
}

/**
 * 得到两个日期之间相差�?��数?�日期�?式为"yyyyMMdd"
 * 
 * @param date1
 *            日�?
 * @param date2
 *            日�?
 * @return 相差天数
 * @throws ParseException
 */
public static int getDaysDiff(String date1, String date2)
		throws ParseException {
	return getDaysDiff(date1, date2, "yyyyMMdd");
}

/**
 * 得到两个日期之间相差�?��数
 * 
 * @param date1
 *            日�?
 * @param date2
 *            日�?
 * @param dateFormat
 *            日期�?�?
 * @return 相差天数
 * @throws ParseException
 */
public static int getDaysDiff(int date1, int date2, String dateFormat)
		throws ParseException {
	return getDaysDiff(String.valueOf(date1), String.valueOf(date2),
			dateFormat);
}

/**
 * 得到两个日期之间相差�?��数?�日期�?式为"yyyyMMdd"
 * 
 * @param date1
 *            日�?
 * @param date2
 *            日�?
 * @return 相差天数
 * @throws ParseException
 */
public static int getDaysDiff(int date1, int date2) throws ParseException {
	return getDaysDiff(String.valueOf(date1), String.valueOf(date2),
			"yyyyMMdd");
}

/**
 * created by wangli 曜日を取得す�?
 * 
 * @param dates
 * @return 曜日
 * @throws ParseException
 * @throws ParseException
 */
public static String getYoubi(String dates) throws ParseException {
	Date date = null;
	date = new SimpleDateFormat("yyyyMMdd").parse(dates);
	return new SimpleDateFormat("EEE", Locale.JAPAN).format(date.getTime());
}

/**
 * 得到当前时间long类型，�?用于insert into [DB]
 */
public static long getLongTime() {
	return System.currentTimeMillis();
}

/**
 * 格式化日�?格式化后可直接insert into [DB],格式化后的日期为??006-10-12 14:42:47
 * <P>
 * 把日期转换成字符串
 */
public static String dateToStr(Date date) {

	if (date == null)
		return "";
	else {
		SimpleDateFormat sdFormat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss", Locale.getDefault());
		String str_Date = sdFormat.format(date);
		return str_Date;
	}
}

/**
 * 格式化后的日期为??006年10�?2
 * <P>
 * 把日期转换成字符串
 */
public static String dateToStrCN(Date date) {

	if (date == null)
		return "";
	else {
		SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy年MM�?d",
				Locale.getDefault());
		String str_Date = sdFormat.format(date);
		return str_Date;
	}
}

/**
 * 格式化后的日期为??006-10-12
 * <P>
 * 把日期转换成字符串
 */
public static String dateToStrEn(Date date) {

	if (date == null)
		return "";
	else {
		SimpleDateFormat sdFormat = new SimpleDateFormat("yyyy-MM-dd",
				Locale.getDefault());
		String str_Date = sdFormat.format(date);
		return str_Date;
	}
}

/**
 * DateUtil.strToDate("2006-10-12") 转换为?�Thu Oct 12 00:00:00 CST 2006
 * <P>
 * 把字符串转换成日期类�?
 */
public static Date strToDate(String str) {
	if (str == null)
		return null;
	DateFormat defaultDate = DateFormat.getDateInstance();
	Date date = null;
	try {
		date = defaultDate.parse(str);
	} catch (ParseException pe) {
	}
	return date;
}

/**
 * 日期计�?
 * 
 * @param date
 *            起始日�?
 * @param yearNum
 *            年增减数
 * @param monthNum
 *            月增减数
 * @param dateNum
 *            日增减数
 */
public static String calDate(String date, int yearNum, int monthNum,
		int dateNum) {
	String result = "";
	try {
		SimpleDateFormat sd = new SimpleDateFormat("yyyy-MM-dd");
		Calendar cal = Calendar.getInstance();
		cal.setTime(sd.parse(date));
		cal.add(Calendar.MONTH, monthNum);
		cal.add(Calendar.YEAR, yearNum);
		cal.add(Calendar.DATE, dateNum);
		result = sd.format(cal.getTime());
	} catch (Exception e) {
		e.printStackTrace();
	}
	return result;
}

private static String delFrontZero(String mord) {
	int im = -1;
	try {
		im = Integer.parseInt(mord);
		return String.valueOf(im);
	} catch (Exception e) {
		return mord;
	}
}

/**
 * DateUtil.parseDate("20070423123754") 转换为?�Mon Apr 23 12:37:54 CST 2007
 * 
 * @param orlTime
 * @return
 */
public static Date parseDate(String orlTime) {
	Date date = null;
	if (orlTime == null || orlTime.length() <= 0) {
		return null;
	}

	if (!(orlTime.length() == 14)) {
		return null;
	}
	SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
	try {
		date = (Date) formatter.parse(orlTime);
	} catch (ParseException e) {
		e.printStackTrace();
	}

	return date;
}

/**
 * DateUtil.parseDate("20070423") 转换为?�Mon Apr 23 00:00:00 CST 2007
 * 
 * @param orlTime
 * @return
 */
public static Date parseDateDB(String orlTime) {
	Date date = null;
	if (orlTime == null || orlTime.length() <= 0) {
		return null;
	}

	if (!(orlTime.length() == 8)) {
		return null;
	}
	SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");
	try {
		date = (Date) formatter.parse(orlTime);
	} catch (ParseException e) {
		e.printStackTrace();
	}

	return date;
}

/**
 * DateUtil.parseCnDate("2006-10-12") 转换为??006年10�?2日
 * 
 * @param orlTime
 * @return
 */
public static String parseCnDate(String orlTime) {
	if (orlTime == null || orlTime.length() <= 0) {
		return "";
	}

	if (orlTime.length() < 10) {
		return "";
	}
	String sYear = orlTime.substring(0, 4);
	String sMonth = delFrontZero(orlTime.substring(5, 7));
	String sDay = delFrontZero(orlTime.substring(8, 10));
	return sYear + "年" + sMonth + "月" + sDay + "日";
}

/**
 * DateUtil.parsePointDate("2006-10-12") 转换为??0.12
 * 
 * @param orlTime
 * @return
 */
public static String parsePointDate(String orlTime) {
	if (orlTime == null || orlTime.length() <= 0) {
		return "";
	}
	String sMonth = delFrontZero(orlTime.substring(5, 7));
	String sDay = delFrontZero(orlTime.substring(8, 10));
	return sMonth + "." + sDay;
}

/**
 * 得到当前月的�?���?��
 * 
 * @param
 * @return
 */
@SuppressWarnings("deprecation")
public static Date getLastDayOfMonth(Date sDate1) {
	Calendar cDay1 = Calendar.getInstance();
	cDay1.setTime(sDate1);
	final int lastDay = cDay1.getActualMaximum(Calendar.DAY_OF_MONTH);
	Date lastDate = cDay1.getTime();
	lastDate.setDate(lastDay);
	return lastDate;
}

/**
 * 得到当前月的第�?��
 * 
 * @param
 * @return
 */
@SuppressWarnings("deprecation")
public static Date getFirstDayOfMonth(Date sDate1) {
	Calendar cDay1 = Calendar.getInstance();
	cDay1.setTime(sDate1);
	final int lastDay = cDay1.getActualMinimum(Calendar.DAY_OF_MONTH);
	Date lastDate = cDay1.getTime();
	lastDate.setDate(lastDay);
	return lastDate;
}

public static long calendarDayPlus(Date d1, Date d2) {
	long milis = calendarMilisPlus(d1, d2);
	milis = milis / 1000 / 60 / 60 / 24;
	return milis;
}

public static long calendarMilisPlus(Date d1, Date d2) {
	return d2.getTime() - d1.getTime();
}
}

